﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;

namespace ChamCongMain.Form
{
    public partial class frmDonVi : DevExpress.XtraEditors.XtraForm
    {
        ChucNang cn;
        public Delegates.RefeshGrid LoadData;
        int id = 0;

        public frmDonVi(ChucNang cn, DonVi dv)
        {
            InitializeComponent();
            this.cn = cn;
            if (this.cn == ChucNang.Them)
            {
                this.Text = "Thêm Đơn Vị";
            }
            else
            {
                this.Text = "Sửa Đơn Vị";
                SetValueForForm(dv);
            }
        }

        private void SetValueForForm(DonVi dv)
        {
            id = int.Parse(dv.MaDonVi.ToString());
            txtTenDonVi.Text = dv.TenDonVi;
            txtEmail.Text = dv.Email;
            txtGhiChu.Text = dv.GhiChu;
            txtSDT.Text = dv.Sdt;
        }

        private DonVi GetValueFromForm()
        {
            DonVi dv = new DonVi();
            dv.MaDonVi = id;
            dv.TenDonVi = txtTenDonVi.Text;
            dv.Sdt = txtSDT.Text;
            dv.GhiChu = txtGhiChu.Text;
            dv.Email = txtEmail.Text;
            return dv;
        }

        private void ClearForm()
        {
            txtEmail.Text = "";
            txtGhiChu.Text = "";
            txtSDT.Text = "";
            txtTenDonVi.Text = "";
            txtTenDonVi.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            DonVi dv = GetValueFromForm();
            DonViBLL cnbll = new DonViBLL();
            if (cn == ChucNang.Them)
            {
                cnbll.InsertDonVi(dv);
                XtraMessageBox.Show("Thêm thành công", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                cnbll.UpdateDonVi(dv);
                XtraMessageBox.Show("Sửa thành công", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            LoadData(cnbll.GetAllDonVi());
            this.Close();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            DonVi dv = GetValueFromForm();
            DonViBLL cnbll = new DonViBLL();
            if (cn == ChucNang.Them)
            {
                cnbll.InsertDonVi(dv);
                XtraMessageBox.Show("Thêm thành công", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                cnbll.UpdateDonVi(dv);
                XtraMessageBox.Show("Sửa thành công", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            LoadData(cnbll.GetAllDonVi());
            ClearForm();
        }

    }
}